package com.ww.grtsmanager.controller;

import com.alibaba.excel.EasyExcel;
import com.ww.grtsmanager.Listener.ExcelListener;
import com.ww.grtsmanager.bean.Subject;
import com.ww.grtsmanager.mapper.SubjectMapper;
import com.ww.grtsmanager.service.SubjectService;
import com.ww.grtsmanager.vo.EduResult;
import com.ww.grtsmanager.vo.ExcelVo;
import com.ww.grtsmanager.vo.SubjectTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/subject")
public class SubjectController {
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private SubjectService subjectService;
    @RequestMapping("/download")
    public void downloadExcel(HttpServletResponse response){

        try {
            //设置响应内容类型
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            //设置响应的编码格式
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("上传文件模板", "UTF-8").replaceAll("\\+", "%20");
            //设置相应头 为excel文件
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ExcelVo.class).sheet("模板").doWrite(getData());
        } catch (IOException e) {
            //失败返回json格式
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            throw new RuntimeException("下载excel模板失败");
        }
    }
    @RequestMapping ("/excelUpload")
    public EduResult excelUpload(MultipartFile file) {
        try {
            List<Subject>list=new ArrayList<Subject>();
            EasyExcel.read(file.getInputStream(), ExcelVo.class, new ExcelListener(subjectMapper,list)).sheet().doRead();
            return EduResult.ok();
        } catch (IOException e) {
            throw new RuntimeException("上传excel失败");
        }
    }
    @RequestMapping("/getSubjectTree")
    public EduResult getSubjectTree(){
        List<SubjectTree> list=subjectService.getSubjectTree();
        return EduResult.ok(list);
    }

    private List<ExcelVo> getData(){
        ExcelVo excelVo1 = new ExcelVo("程序开发","javase基础");
        ExcelVo excelVo2 = new ExcelVo("程序开发","spring boot入门");
        ExcelVo excelVo3 = new ExcelVo("网页设计","网页基础");
        List<ExcelVo>list = new ArrayList<ExcelVo>();
        list.add(excelVo1);
        list.add(excelVo2);
        list.add(excelVo3);
        return list;
    }
    @RequestMapping("/addSubject")
    public EduResult addSubject(Subject subject){
        subjectService.addSubject(subject);
        return EduResult.ok();
    }
    @RequestMapping("/deleteSubject")
    public EduResult deleteSubject(@PathVariable Integer id){
        subjectService.deleteSubject(id);
        return EduResult.ok();
    }
}
